#include "./analog.h"
#include "../device/adc.h"
#include "../device/gpio.h"
#include "../device/dma.h"
#include "./cmd_uart.h"

namespace driver {

using namespace device::gpio;
using namespace device::adc;
using namespace device::dma;

using KPA = PC<0>; // AIN10
using MOTOR_CHECK = PC<1>; // AIN11

using ADC_DMA = Dma<DMA1_BASE, 1>;
using ADC_DMA_CHAN = DmaChan<DMA1_Channel1_BASE, 0, 1>;

Analog::Analog() {

    RCC->APB2ENR |= RCC_APB2ENR_IOPCEN + RCC_APB2ENR_ADC1EN;
    RCC->AHBENR |= RCC_AHBENR_DMA1EN;
    init<ADC1_BASE, ADC_DMA, ADC_DMA_CHAN, 10, 11>(m_values);
}

uint16_t Analog::kpa_value() {
    float val = m_values[0] * 0.5323f;
    if (val < 388.25f) {
        return 0;
    }
    return val - 388.25f;
}

}

